package group.sicher.core.web.parse;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * Created by bootvue@gmail.com
 * Date 2019-04-03 23:29
 * bigdecimal  两位精度
 */
public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (ObjectUtils.isEmpty(value)) {
            gen.writeString("0.00");
        } else {
            gen.writeString(String.valueOf(value.setScale(2, BigDecimal.ROUND_HALF_UP)));
        }
    }
}
